/**
 * ------------------------------------------------------------*
 *          COPYRIGHT (C) 2006 BOCO Inter-Telecom INC          *
 *   CONFIDENTIAL AND PROPRIETARY. ALL RIGHTS RESERVED.        *
 *                                                             *
 *  This work contains confidential business information       *
 *  and intellectual property of BOCO  Inc, Beijing, CN.       *
 *  All rights reserved.                                       *
 * ------------------------------------------------------------*
 *
 *
 */
/**
 *Revision Information:
 *
 *@version 1.0 2006-2-5 release(zhangxu)
 */

package com.yun.util.yun.utils.date;

import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class TimeFormatHelper {
	public static final String TIME_FORMAT_A = "yyyy-MM-dd HH:mm:ss";
	public static final String TIME_FORMAT_B = "yyyyMMddHHmmss";
	public static final String TIME_FORMAT_C = "yyyy-MM-dd HH:mm:ss:SSS";
	public static final String DATE_FORMAT = "yyyy-MM-dd";
	public static final String TIME_FORMAT = "HH:mm:ss";
	public static final String TELCORDIA_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static final String YEAR_FORMAT = "yyyy";

	private TimeFormatHelper() {
	}

	public static String getFormatDate(Date date, String format) {
		String dateStr = null;
		try {
			if (date != null) {
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
				TimeZone timezone = TimeZone.getTimeZone("GMT+08:00");
				simpleDateFormat.setTimeZone(timezone);
				dateStr = simpleDateFormat.format(date);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return dateStr;
	}

	public static Date convertDate(String dateStr, String format) {
		Date date = new Date();
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
			date = simpleDateFormat.parse(dateStr);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return date;
	}

	public static Date convertDate(String dateStr) {
		if (StringUtils.trimToNull(dateStr) == null) {
			return null;
		}
		Date date = null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				TimeFormatHelper.TIME_FORMAT_A);
		TimeZone timezone = TimeZone.getTimeZone("GMT+08:00");
		simpleDateFormat.setTimeZone(timezone);
		try {
			date = simpleDateFormat.parse(dateStr);
		} catch (ParseException ex) {
			simpleDateFormat = new SimpleDateFormat(
					TimeFormatHelper.DATE_FORMAT);
			try {
				simpleDateFormat.setTimeZone(timezone);
				date = simpleDateFormat.parse(dateStr);
			} catch (ParseException e) {
				try {
					date = new Date(Long.valueOf(dateStr));
				} catch (Exception ec) {
					try {
						date = simpleDateFormat.parse("1999-01-01");
					} catch (ParseException e1) {
						e1.printStackTrace();
					}
				}
			}
		}
		return date;
	}

	public static Date checkIsDate(String dateStr) {
		Date date = null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				TimeFormatHelper.TIME_FORMAT_A);
		try {
			date = simpleDateFormat.parse(dateStr);
		} catch (ParseException ex) {
			simpleDateFormat = new SimpleDateFormat(
					TimeFormatHelper.DATE_FORMAT);
			try {
				date = simpleDateFormat.parse(dateStr);
			} catch (ParseException e) {
				try {
					date = new Date(Long.valueOf(dateStr));
				} catch (Exception ec) {
					ec.printStackTrace();
				}
			}
		}
		return date;
	}
}
